<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
require_once("db.php");
require_once("db_data.php");

function db_init()
{

   global $wpdb;

   $table_name = $wpdb->prefix . DISASTER_EVENTS_TABLE;
   if($wpdb->get_var("show tables like '$table_name'") != $table_name) 
   {
	  
	  $sql = "CREATE TABLE " . $table_name . " (
	  event_id mediumint(9) NOT NULL AUTO_INCREMENT PRIMARY KEY,
	  location VARCHAR(50),
	  geocode VARCHAR(50),
	  name VARCHAR(15) NOT NULL,
	  start_date date NOT NULL,
	  description text NOT NULL,
	  shipping_address VARCHAR(150) NOT NULL,
	  end_date date,
	  remove_flag BOOL DEFAULT FALSE,
	  active_flag BOOL DEFAULT TRUE
	  
	);";
	

	$wpdb->query($sql);

    }
	
    //Run query, create disaster event table

	//Query to create a table for supplies 
	//(may want to take this outside of the IF loop?)
	
    $table_name = $wpdb->prefix . SUPPLIES_TABLE;

   if($wpdb->get_var("show tables like '$table_name'") != $table_name)
   {
   
	$sql = "CREATE TABLE " . $table_name . " (
	event_id mediumint(9) NOT NULL,
	shipment_id mediumint(9) NOT NULL, 
	type VARCHAR( 25 ) NOT NULL,
	unit varchar(15) NOT NULL,
	amount_needed BIGINT( 100 ) NOT NULL,
	FOREIGN KEY (event_id) REFERENCES " . $wpdb->prefix . DISASTER_EVENTS_TABLE . " (event_id),
	FOREIGN KEY (shipment_id) REFERENCES " . $wpdb->prefix . SHIPMENTS_TABLE . " (shipment_id)

	);";

	
	//Run query, create supplies table
	$wpdb->query($sql);
   }
	
   $table_name = $wpdb->prefix . SHIPMENTS_TABLE;

   if($wpdb->get_var("show tables like '$table_name'") != $table_name) 
   {
	//Test code for shipments table
	
	$sql = "CREATE TABLE " . $table_name . " (
	event_id mediumint(9) NOT NULL,
	shipment_id mediumint(9) NOT NULL AUTO_INCREMENT PRIMARY KEY,
	date_sent date,
	date_received date,
	FOREIGN KEY (event_id) REFERENCES " . $wpdb->prefix . DISASTER_EVENTS_TABLE . " (event_id)
	);";
	
	
	//Run query, create shipments table
	$wpdb->query($sql);


    }

    $table_name = $wpdb->prefix . USER_CONTRIBUTIONS_TABLE;
    if($wpdb->get_var("show tables like '$table_name'") != $table_name)
   {
	//Test code for shipments table

	$sql = "CREATE TABLE " . $table_name . " (
	user_id bigint(20) NOT NULL,
	shipment_id mediumint(9) NOT NULL,
	submission_date date NOT NULL,
	amount bigint(100) NOT NULL,
	type varchar(25) NOT NULL,
	received bool DEFAULT FALSE,
	FOREIGN KEY (shipment_id) REFERENCES " . $wpdb->prefix . SHIPMENTS_TABLE . " (shipment_id),
	FOREIGN KEY (user_id) REFERENCES " . $wpdb->prefix . "users (ID)
	);";



	//Run query, create shipments table
	$wpdb->query($sql);



    }
	
			add_data();


	 
}











?>
